package com.aiit.carinsurance2.controller;

import com.aiit.carinsurance2.pojo.*;
import com.aiit.carinsurance2.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class FinallylistinfoController {



    @Autowired
    private FinallylistinfoService finallylistinfoService;

    @Autowired
    private CarService carService;
    @Autowired
    private ToubaodanService toubaodanService;
    @Autowired
    private UserService userService;
    @Autowired
    private InsuredService insuredService;

    @Autowired
    private FinallylistService finallylistService;

    @GetMapping("/insertlist")
    @ResponseBody
    public Integer insertlist(HttpSession session){
        Finallylist finallylist=new Finallylist();
        Object obj = session.getAttribute("user");
        User u = (User)obj;
        Integer uid = u.getUid();
        Insured insured=insuredService.querybyuid(uid);
        Car car =carService.querybyuid(uid);
        Toubaodan toubaodan=toubaodanService.querybyuid(uid);
        int buid=insured.getBuid();
        int carid=car.getCid();
        int tid=toubaodan.getId();
        finallylist.setUid(uid);
        finallylist.setBuid(buid);
        finallylist.setCarid(carid);
        finallylist.setTid(tid);
        finallylist.setStates("未打印");
        int rlt=finallylistService.insert(finallylist);
        System.out.println(finallylist);
        return rlt;
    }

    @GetMapping("/tofinallist")
    @ResponseBody
    public Integer tofinallst(){
        int msg=1;
        return msg;
    }

    @GetMapping("/finallylistinfo")
    public ModelAndView finallylistinfo(HttpSession session){
        Object obj = session.getAttribute("user");
        User u = (User)obj;
        Integer uid = u.getUid();
        ModelAndView mav=new ModelAndView();
        Finallylistinfo finallylistinfo=finallylistService.querylistbyuid(uid);
        List<Finallylistinfos> finallylistinfos=finallylistService.querylistinfo(uid);
        System.out.println("输出："+finallylistinfo);
        mav.addObject("querylist",finallylistinfo);
        mav.addObject("querylistinfo",finallylistinfos);
        mav.setViewName("finallylistinfo");
        return mav;
    }

    @GetMapping("finallylists")
    public String tofin(){

        return "fuwudating";
    }

//    @GetMapping("pays")
//    @ResponseBody
//    public Integer topays(){
//        int i=1;
//        return i;
//    }

    @GetMapping("pay")
    public String pay(){return "pay";}

    @PostMapping("/insertpay")
    public String postinsert(HttpSession session,Pay pay){
        Object obj = session.getAttribute("user");
        User u = (User)obj;
        Integer uid = u.getUid();
        String states="已支付";
        pay.setUid(uid);
        pay.setStates(states);
        Toubaodan toubaodan=userService.querybyuid(uid);
        pay.setPayall(toubaodan.getAllpay());
        System.out.println(pay);
        int rlt=userService.insert(pay);
        return "fuwudating";
    }
}
